package com.dayaokeji.rhythmschoolstudent.client.home.join

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.R.menu.course
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment
import com.dayaokeji.rhythmschoolstudent.client.home.adapter.JoinMeetingListAdapter
import com.dayaokeji.rhythmschoolstudent.event.UpdateMeetingEvent
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils
import com.dayaokeji.rhythmschoolstudent.wiget.ConfirmDialog
import com.dayaokeji.server_api.ApiUtils
import com.dayaokeji.server_api.ServerResponse
import com.dayaokeji.server_api.api.MeetingApi
import com.dayaokeji.server_api.domain.Meeting
import org.greenrobot.eventbus.EventBus
import retrofit2.Call

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/27
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 **
 *********************************************************************
 */
class JoinMeetingFragment: ListFragment<Meeting,BaseViewHolder>() , Search{

    private var key: String? = null
    private val joinMeetingListAdapter: JoinMeetingListAdapter by lazy { JoinMeetingListAdapter() }
    private val meetingApi: MeetingApi by lazy { ApiUtils.getApi(MeetingApi::class.java) }
    private var addMeetingCall: Call<ServerResponse<Void>>? = null

    companion object {
        fun newInstance(): JoinMeetingFragment  = JoinMeetingFragment()
    }

    override fun createCall(offset: Int, max: Int): Call<*>? {
        if (key == null) return null
        return ApiUtils.getApi(MeetingApi::class.java).searchMeetingById(
                key,
                offset,
                max
        )
    }

    override fun search(key: String) {
        this.key = key

        onRefresh()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        adapter = joinMeetingListAdapter

        joinMeetingListAdapter.onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener{ baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            val meeting = baseQuickAdapter.data[i] as Meeting
            when (view.id) {
                R.id.tv_join_course -> {
                    val confirmDialog = ConfirmDialog.newInstance(ConfirmDialog.ConfirmInfo(content = "是否加入<b><font color='#0074FE'> ${meeting.name}</font> </b> 会议？"))
                    confirmDialog.show(childFragmentManager, "add_meeting")
                    confirmDialog.setConfirmClickListener(object : ConfirmDialog.ConfirmClickListener {
                        override fun click() {
                            addMeeting(meeting.id)
                        }
                    })
                }
            }
        }
    }

    private fun addMeeting(meetingId: Int) {
        val params = HashMap<String, Any>()
        params["id"] = meetingId
        params["userId"] = UserUtils.getUserInfo()?.id!!
        addMeetingCall = meetingApi.addMeeting(params)
        addMeetingCall?.enqueue(object : ServerCallback<Void>() {
            override fun onResponse(success: Boolean, response: ServerResponse<Void>?) {
                if (success) {
                    ToastUtils.success("加入会议成功")
                    EventBus.getDefault().post(UpdateMeetingEvent())
                }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        addMeetingCall?.cancel()
    }
}